;
(function() {
	var skill = {
		save: function() {
			db.update(config.dbname.goodsArgsMap, this._id, this);
		},
		destroy: function() {
			db.remove(config.dbname.goodsArgsMap, this._id);
		},
		setInfo: function(info) {
			//属性信息表
			var args = []
			var paths_value = [];
			if (Array.isArray(info.args)) { //数据校验与整理
				info.args.forEach(function(arg_info) {
					if (!arg_info.title) {
						throwCusError("属性名不可为空")
					}
					if (!Array.isArray(arg_info.items)) {
						arg_info.items = [];
						// throwCusError("属性“" + arg_info.title + "”无选项")
					}
					var items = [];
					var paths_value_item = [];
					arg_info.items.forEach(function(arg_info_item) {
						if (arg_info_item.value) {
							//类型校准
							if ($Object.hasValue(GoodsArgsMapCon.TYPE, arg_info_item.type)) {
								if (!arg_info_item.value) {
									throwCusError("属性项的描述不可为空")
								}
								var item = {
									type: arg_info_item.type,
									value: arg_info_item.value
								};
								items.push(item);
								if (item.type === GoodsArgsMapCon.TYPE.图片) {
									item.src = arg_info_item.src;
								} else if (item.type === GoodsArgsMapCon.TYPE.颜色) {
									item.color = arg_info_item.color;
								}
								paths_value_item.push(item.value)
							}
						}
					});
					args.push({
						title: arg_info.title,
						items: items
					});
					paths_value.push(paths_value_item);
				})
			}
			this.info.args = args;

			//直接到商品中存储这个信息
			//商品-属性表
			var goods_arg_array = [];
			if (Array.isArray(info.goods_arg_array)) {
				var _goods_has_map = {};
				var _args_has_map = {};
				info.goods_arg_array.forEach(function(goods_arg_info) {
					var goods_id = goods_arg_info.goods_id;
					if (_goods_has_map[goods_id]) { //重复项，此数据无效
						return
					}
					_goods_has_map[goods_id] = true;
					var goods = GoodsCon.getAlone(goods_id);
					if (!goods) { //商品不存在，此条数据无效
						return;
					}
					var arg_paths = goods.using_goods_args || []; // Array.isArray(goods_arg_info.arg_paths) ? goods_arg_info.arg_paths : [];

					//检查路径的正确性
					if (!arg_paths.every(function(path_item, index) {
							var paths_value_item = paths_value[index];
							return paths_value_item && paths_value_item.indexOf(path_item) !== -1
						})) {
						//路径有误，删除路径信息
						arg_paths = goods.using_goods_args = [];
						goods.save();
					}
					if (arg_paths.length) {
						var arg_paths_str = arg_paths.join("\n");
						if (_args_has_map[arg_paths_str]) {
							throwCusError("同类商品间存在相同的可选属性选项，请重新选择商品的可选属性");
						}
						_args_has_map[arg_paths_str] = true;
					}

					goods_arg_array.push({
						goods_id: goods_id,
						arg_paths: arg_paths
					});
				});
			};
			console.log(info.goods_arg_array, goods_arg_array)
			this.info.goods_arg_array = goods_arg_array;

			this.save();
		}
	};
	module.exports = skill;

	var db = require("../db/index");
	var GoodsArgsMapCon = require("../goods_args_map/_con");
	var GoodsCon = require("../goods/_con");
	var UserCon = require("../../core/user/_con");
	var BusCon = require("../../core/bus/_con");
	var AchCon = require("../ach/_con");
	var ClassifyCon = require("../bus.classify/_con");
	var TagCon = require("../bus.tag/_con");
	var LeagueCon = require("../league/_con");
	var BuffCon = require("../buff/_con");
	var BusUserBindCon = require("../bus_user_bind/_con");
	var BusGuideCon = require("../bus.guide/_con");
	var CommentCon = require("../comment/_con");
	var config = require("../_include/config");
	var data_cut = require("../_include/data_cut");
	var helper = require("../_include/helper");
	var md5 = require("../_include/md5");
	var $Object = require("../_include/$.Object");
	var $Array = require("../_include/$.Array");
}());